<?php

namespace app\services;


use app\models\ModBaseInfo;
use core\base\BaseService;
use core\exception\CommonException;
use core\lib\Handle;

class RecruitService extends BaseService
{
    public function search($args = [])
    {
        try {
            $data = ModBaseInfo::find()->search($args);
            $count = ModBaseInfo::find()->search(array_merge($args, ['count' => 1]));
            $pageCount = ceil($count / 25);
            return [
                'code' => 200,
                'msg' => 'success',
                'data' => [
                    'list' => $data,
                    'count' => $pageCount,
                    'currentPage' => empty($args['page']) ? 1 : $args['page'],
                ]
            ];
        } catch (CommonException $e) {
            return Handle::handleException($e);
        }
    }

    public function editOne($id, $args = [])
    {
        try {
            if (!empty($args)) {
                $args['is_checked'] = 1;
                if (empty($id)) {
                    // 新增一条
                    $baseInfo = new ModBaseInfo();
                    $baseInfo->setAttributes($args);
                    $bool = $baseInfo->save();
                } else {
                    ModBaseInfo::updateAll($args, ['id' => $id]);
                }
            }
            return [
                'code' => 200,
                'msg' => 'success',
            ];
        } catch (\Exception $e) {
            return Handle::handleException($e);
        }
    }

    public function changeCheckStatus($id, $status)
    {
        try {
            ModBaseInfo::updateAll(['is_checked' => $status], ['id' => $id]);
            return [
                'code' => 200,
                'msg' => 'success',
            ];
        } catch (\Exception $e) {
            return Handle::handleException($e);
        }
    }

    public function export()
    {
        ini_set('memory_limit', '1024M');
        $baseInfos = ModBaseInfo::find()->asArray()->all();
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $sheet = $objPHPExcel->getActiveSheet();
        $this->setTitle($sheet);
        $this->setContent($sheet, $baseInfos);
        $fileName = 'ResumeData';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'. $fileName .'.xlsx"');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        $objPHPExcel->disconnectWorksheets();
        \Yii::$app->end();
    }

    /**
     * @param $sheet /PHPExcel_Worksheet
     */
    public function setTitle($sheet)
    {
        $sheet
            ->setCellValue("A1", 'resume_id')
            ->setCellValue("B1", 'file_name')
            ->setCellValue("C1", 'file_real_name')
            ->setCellValue("D1", 'name')
            ->setCellValue("E1", 'mobile')
            ->setCellValue("F1", 'email');
    }

    public function setContent($sheet, $data)
    {
        $cnt = count($data);
        for($i=0; $i<$cnt; $i++){
            $d = $data[$i];
            // 下标从2开始
            $index = $i + 2;
            $sheet
                ->setCellValue("A{$index}", $d['resume_id'])
                ->setCellValue("B{$index}", $d['file_name'])
                ->setCellValue("C{$index}", $d['real_file_name'])
                ->setCellValue("D{$index}", $d['name'])
                ->setCellValue("E{$index}", $d['mobile'])
                ->setCellValue("F{$index}", $d['email']);
        }
    }
}